package com.eascs.admin.o2o.compensatory.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.o2o.compensatory.service.O2oDCLoanService;
import com.eascs.workflow.common.service.sequence.SequenceService;

/**
 * 自动生成 代偿编号和归档编号
 * @author user
 *
 */
@Component
public class GenerationCodeUtil{

	@Reference
	private SequenceService sequenceService;
	
	@Autowired
	O2oDCLoanService service;
	
	//类型type:CD 代偿，GD 归档 、省份province(中文的省份名字：如广东、广西)、length:生成编码的长度
	public String generationCode(String type,String province,int length){
		String intoCabinetCode = "";
		if(type == null || province == null){
		throw new RuntimeException();
		}
		Calendar cal = Calendar.getInstance();
		String today = new SimpleDateFormat("yyyyMM").format(cal.getTime()); 
		// 根据省份查询省份简称
		String provinceJC = service.findJCByProvince(province);
		// 获取编号
		String createCode = sequenceService.getSeq(type, length);
		String splitCode[] = createCode.split("-");
		intoCabinetCode =  type + "-" + provinceJC + "-" + today + "-" + splitCode[1];
		return intoCabinetCode;
	}
}
